package com.kgc.fk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgapi.pojo.CGDDPojo;
import com.kgc.collection.pojo.PrePay;
import com.kgc.collection.pojo.PrePayDetail;
import com.kgc.collection.pojo.SalesAdvance;
import com.kgc.fk.dao.PrePayDao;
import com.kgc.fk.feign.CwCGDDProviderFeign;
import com.kgc.fk.service.PrePayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PrePayServiceImpl implements PrePayService {

    private Logger logger = LoggerFactory.getLogger(PrePayServiceImpl.class);

    @Resource
    private PrePayDao prePayDao;


//    @Resource
//    private CwCGDDProviderFeign cwCGDDProviderFeign;

    @Override
    public ResponseMsg getPrePayPage(Map<String, Object> params) {
        logger.info("getPrePayPage  params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<PrePay> pageInfo= new PageInfo<>();
        pageInfo.setPageNo(params.get("pageNo")==null?1:Integer.parseInt(params.get("pageNo").toString()));
        pageInfo.setPageSize(params.get("pageSize")==null?10:Integer.parseInt(params.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<PrePay> list=prePayDao.getPrePayList(params);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("getPrePayPage  pageInfo is{} ,params{}",pageInfo.toString(),params);
        return responseMsg;
    }

    @Override
    public ResponseMsg addPrePay(Map<String, Object> map) {
        logger.info("addPrePay map:" + map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) map.get("details");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String dateString = format.format(date);

        StringBuilder str = new StringBuilder(String.valueOf(prePayDao.countNum(dateString)));
        for (int i = str.length(); i<4 ; i++) {
            str.insert(0, "0");
        }

        String code = "CGYF-"+ dateString + "-" + str.toString();
        map.put("docCode", code);



        Object createId = map.get("createBy");
        map.put("createDate", date);

        int result = prePayDao.addPrePay(map);

        logger.info("addOtherPay result:{}",result);
        if (result <= 0) {
            return responseMsg;
        }
        for (Map<String, Object> info : infoList) {
            info.put("createDate", date);
            info.put("mainCode", code);
            info.put("createBy", createId);
            result = prePayDao.addPrePayDetail(info);
            if (result <= 0) {
                return responseMsg;
            }
        }
        responseMsg.setData(code);
        responseMsg.setCode(200);
        logger.info("addPrePay end response:{} , map{}",responseMsg,map);
        return responseMsg;
    }

    @Override
    public ResponseMsg updatePrePay(Map<String, Object> params) {
        logger.info("updatePrePay params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = prePayDao.updatePrePay(params);
        logger.info("updatePrePay result:{}",result);
        if (result > 0) {
            responseMsg.setCode(200);
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getPrePay(Map<String, Object> params) {
        logger.info("getPrePay params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);

        List<PrePay> list=prePayDao.getPrePayList(params);
        logger.info("getPrePay list result:{}",list);

        for (PrePay prePay : list) {
            String code= prePay.getDocCode();
            List<PrePayDetail> detailList = prePayDao.getPrePayDetail(code);
            prePay.setList(detailList);
        }
        responseMsg.setData(list.get(0));
        logger.info("getPrePay end params{} response:{}",params,responseMsg);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiPrePay(Map<String, Object> params) {
        logger.info("modiPrePay params:" + params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) params.get("details");
        Date date = new Date();
        params.put("updateTime" , date);

        String code = params.get("docCode").toString();
        int result = prePayDao.modiPrePay(params);
        logger.info("modiPrePay result:{}",result);

        result = prePayDao.delPrePayInfo(code);
        logger.info("delPrePay code{} result:{}",code,result);

        for (Map<String, Object> info : infoList) {
            info.put("createDate" , date);
            info.put("createBy" , params.get("updateBy"));
            info.put("mainCode" , code);
            result = prePayDao.addPrePayDetail(info);
        }
        responseMsg.setCode(200);
        logger.info("modiPrePay end response:{}",responseMsg);
        return responseMsg;

    }

    @Override
    public ResponseMsg getPrePayList(Map<String, Object> params) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        List<PrePay> list=prePayDao.getPrePayList(params);
        logger.info("getPrePay list result:{}",list);
        responseMsg.setData(list);
        responseMsg.setCode(200);
        logger.info("getPrePay end params{} response:{}",params,responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg calPrePayRefund(String code , Map<String, Object> cgddPojo) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);

        Map<String,Object> params = new HashMap<>();
        params.put("code", code);
        List<PrePay> list=prePayDao.getPrePayList(params);
        PrePay prePay = list.get(0);
        String ddCode = prePay.getSourceSheet();
        logger.info("getPrePay list result:{}",list);

        double money = (double) cgddPojo.get("adv_amount") - (double) cgddPojo.get("use_amount");
        BigDecimal bigDecimal = new BigDecimal(money).setScale(2, RoundingMode.HALF_UP);
        double roundedMoney = bigDecimal.doubleValue();
        responseMsg.setData(roundedMoney);
        responseMsg.setCode(200);
        logger.info("calPrePayRefund end code{} response:{}",code,responseMsg);
//        responseMsg.setData(50000);
        return responseMsg;
    }


}
